package com.csd.serviceImpl;

import java.util.List;

import com.csd.dao.ProjectDAO;
import com.csd.dao.ScoreDAO;
import com.csd.exception.*;
import com.csd.helper.ProjectState;
import com.csd.pojo.Project;
import com.csd.pojo.Score;
import com.csd.service.IScoreService;


/**
 * 
 * Service：分数
 * @author csd
 * @version 1.0
 * 
 * <br> 修订时间：2009-12-17
 * <br> 修改原因：创建文件
 * <br> 修改人：刘世彬
 * 
 * <br> 
 * 
 */
public class ScoreService implements IScoreService {
	private ScoreDAO scoreDAO;
	
	private ProjectDAO projectDAO;

	public ProjectDAO getProjectDAO() {
		return projectDAO;
	}

	public void setProjectDAO(ProjectDAO projectDAO) {
		this.projectDAO = projectDAO;
	}

	public ScoreDAO getScoreDAO() {
		return scoreDAO;
	}

	public void setScoreDAO(ScoreDAO scoreDAO) {
		this.scoreDAO = scoreDAO;
	}

	/**
	 * 查看一个工程的分数
	 * @param projectID
	 * @return Score
	 */
	public Score getScoreByProject(Integer projectID) {
		Score score = null;

		try {
			List list = scoreDAO.findByProperty("project.id", projectID);
			if (list.size() >= 0)
				score = (Score) list.get(0);

		} catch (RuntimeException re) {
					throw new GetException(re, "查询工程整个过程成绩异常");
				}

		return score;
	}
	
	
	/**
	 * 管理员添加成绩
	 * @param projectID 项目id
	 * @param answerScore 答辩得分
	 * @param phaseScore 阶段得分
	 * @param stage 阶段
	 * @return
	 */
	public boolean scoreAddOfManager(Integer projectID, Integer answerScore, Integer phaseScore, String stage){
		boolean bl = false;
		Score score = null;
		Project project= null;
		try {
			score = score ;
			score = scoreDAO.findByProjectID(projectID);
			project = projectDAO.findById(projectID);
			if (score != null && project!=null) {
				//前期申请
				if (stage.equals(ProjectState.beforeStage)) {
					score.setProphasescore(phaseScore);
					if (answerScore != -1) {
						score.setArscore(answerScore);   //带有答辩成绩
					}
					project.setState(ProjectState.proHaveScore); //进入登录成绩状态
				}else 
				//中期检查
				if (stage.equals(ProjectState.middleStage)) {
					score.setMetaphasescore(phaseScore);
					project.setState(ProjectState.metaphaseEnd);  //中期评审结束
				}else
				//末期结题
				if (stage.equals(ProjectState.afterStage)) {
					score.setTelophasescore(phaseScore);
					if (answerScore != -1) {
						score.setArscore(answerScore);		 //带有答辩成绩
						project.setFinalscore(""+phaseScore);
					}
					project.setState(ProjectState.projectEnd);  //项目结束
				}
				scoreDAO.update(score);
				projectDAO.update(project);
				bl = true;
			}
		} catch (RuntimeException re) {
			throw new UpdateException(re, "管理员添加成绩异常");
		}
		return bl;
	}
}
